package com.jswdwsx.common.utils;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

@Component
public class Utils {

    public static boolean isNumeric(String s) {
        if (s != null && !"".equals(s.trim())) {
            return s.matches("^[0-9]*$");
        } else {
            return false;
        }
    }

    public static String genDateTimeStr() {

        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        String year = String.valueOf(cal.get(Calendar.YEAR));
        String month = supplyZero(String.valueOf(cal.get(Calendar.MONTH) + 1));
        String day = supplyZero(String.valueOf(cal.get(Calendar.DAY_OF_MONTH)));
        String hour = supplyZero(String.valueOf(cal.get(Calendar.HOUR_OF_DAY)));
        String min = supplyZero(String.valueOf(cal.get(Calendar.MINUTE)));
        String second = supplyZero(String.valueOf(cal.get(Calendar.SECOND)));

        return year + month + day + hour + min + second;
    }

    public static String supplyZero(String str) {
        if (str.length() == 1) {
            return "0" + str;
        }
        return str;
    }

    public static String supplyDoubleZero(String str) {
        if (str.length() == 1) {
            return "00" + str;
        } else if (str.length() == 2) {
            return "0" + str;
        }
        return str;
    }

    public static String getRandNumber(Integer number) {
        String uuid = UUID.randomUUID().toString();
        String rand = "";
        int i = 0;
        while (i < uuid.length()) {
            char c = uuid.charAt(i);
            if (isNumeric(String.valueOf(c))) {
                rand += c;
            }
            if (rand.length() >= number) {
                break;
            }
            i++;
        }
        return rand;
    }

    public static String getOrderNumber(Integer financeCode, Integer orderChannel, Integer orderType) {
        String n1 = "", n2 = "", n3 = "";
        switch (financeCode) {
            case 1:
                n1 = "L"; // Local
                break;
            case 2:
                n1 = "E"; // Export
                break;
            case 3:
                n1 = "I";
                break; // Internal
            default:
                break;
        }
        switch (orderChannel) {
            case 1:
                n2 = "P"; // POS
                break;
            case 2:
                n2 = "C"; // Online Self-Service
                break;
            case 3:
                n2 = "A"; // customer service
                break;
            case 4:
                n2 = "E"; // ERP
                break;
            case 5:
                n2 = "X"; // 公众号
                break;
            case 6:
                n2 = "W"; // 网站
                break;
            default:
                break;
        }
        switch (orderType) {
            case 1:
                n3 = "R"; // Retail
                break;
            case 2:
                n3 = "W"; // Whole Sale
                break;
            case 3:
                n3 = "E"; // Refund
                break;
            default:
                break;
        }

        String str = genDateTimeStr();
        String rand = getRandNumber(4);
        return n1 + n2 + n3 + str + rand;
    }

    public static ResponseEntity<byte[]> getResponseEntity(ByteArrayOutputStream baos, String fileName) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        try {
            headers.setContentDispositionFormData("attachment", new String(fileName.getBytes("UTF-8"), "ISO8859-1"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return new ResponseEntity<byte[]>(baos.toByteArray(), headers, HttpStatus.CREATED);
    }

    /**
     * 创建指定数量的随机字符串
     *
     * @param numberFlag 是否是数字
     * @param length
     * @return
     */
    public static String createRandom(boolean numberFlag, int length) {
        StringBuilder retStr;
        String strTable = numberFlag ? "1234567890" : "1234567890abcdefghijkmnpqrstuvwxyz";
        int len = strTable.length();
        boolean bDone = true;
        do {
            retStr = new StringBuilder();
            int count = 0;
            for (int i = 0; i < length; i++) {
                double dblR = Math.random() * len;
                int intR = (int) Math.floor(dblR);
                char c = strTable.charAt(intR);
                if (('0' <= c) && (c <= '9')) {
                    count++;
                }
                retStr.append(strTable.charAt(intR));
            }
            if (count >= 2) {
                bDone = false;
            }
        } while (bDone);

        return retStr.toString();
    }

    public static String createNumber(int length) {
        Random random = new Random();
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < length; i++) {
            result.append(random.nextInt(10));
        }
        return result.toString();
    }

    public static String currencyByCode(String code) {
        String output;
        switch (code) {
            case "NZD":
            case "AUD":
                output = "$";
                break;
            case "CNY":
                output = "￥";
                break;
            default:
                output = "";
                break;
        }
        return output;
    }

    public static Integer currencyIdByCode(String code) {
        Integer currencyId;
        switch (code) {
            case "NZD":
                currencyId = 1;
                break;
            case "AUD":
                currencyId = 3;
                break;
            case "CNY":
            case "RMB":
                currencyId = 2;
                break;
            default:
                currencyId = 0;
                break;
        }
        return currencyId;
    }

    public static String currencyCodeById(Integer currencyId) {
        String currencyCode;
        switch (currencyId) {
            case 1:
                currencyCode = "NZD";
                break;
            case 2:
                currencyCode = "CNY";
                break;
            case 3:
                currencyCode = "AUD";
                break;
            default:
                currencyCode = "NZD";
                break;
        }

        return currencyCode;
    }


}
